import {getToken, getUserInfo, setUserInfo, setToken, removeUserInfo, removeToken} from "../../utils/auth";

const state = {
  token: getToken(),//token初始值从cookie中获取
  userInfo: getUserInfo(),//用户信息初始值从cookie中获取
  routeFlag: false,
};
const mutations = {
  SET_T0KEN(state, token) {
    state.token = token;
  },
  SET_USER_INFO(state, userInfo) {
    state.userInfo = userInfo;
  },
  SET_ROUTE_FLAG(state, routeFlag) {
    state.routeFlag = routeFlag;
  }

};
const actions = {
  setToken({commit}, token) {
    commit('SET_T0KEN', token);
    setToken(token);
  },
  setUserInfo({commit}, userInfo) {
    commit('SET_USER_INFO', userInfo);
    setUserInfo(userInfo);
  },
  setRouteFlag({commit}, routeFlag) {
    commit('SET_ROUTE_FLAG', routeFlag);
  },
  signOut({commit}) {
    commit('SET_T0KEN', '');
    commit('SET_USER_INFO', {});
    removeToken();
    removeUserInfo();
  }
};
export default {
  namespaced: true,
  state,
  actions,
  mutations
}
